home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15315 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  87 lines

  1. Path: news.duke.edu!stking
  2. From: stking@acpub.duke.edu (Steve King)
  3. Newsgroups: comp.lang.c
  4. Subject: Problem w/simple linked list
  5. Date: 18 Apr 1996 13:02:34 GMT
  6. Organization: Duke University, Durham, NC, USA
  7. Message-ID: <4l5eha$5qa@news.duke.edu>
  8. NNTP-Posting-Host: carr3.acpub.duke.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Folks, I'm forgetting something here. Here's my problem. I'm
  12. trying to compare a name I input with the names already
  13. stored in nodes in my linked list. If there's a match,
  14. I want to delete that node. However, I can't ever seem to
  15. get a match using "strcmp()."  
  16.  
  17. Here's the relevant code:
  18.  
  19.             /* Assign name to whatever node I'm working on in */
  20.             /* the linked list....                            */
  21.  
  22.             printf("Please enter the owner's name: ");
  23.             fgets(current->patient.name, MAX_NAME,stdin);
  24.             NoNewLine(current->patient.name);
  25.                          .......
  26.  
  27.          /* Prompt for name to delete, pass it to the  */
  28.          /* function "FindInList()" which should return*/
  29.          /* a pointer to the previous node if there's  */
  30.          /* a match, otherwise NULL. But it always     */
  31.          /* returns NULL, even though I know the name  */
  32.          /* I enter is in the list! Why?               */               
  33.          case 3 :
  34.             printf("Enter name to delete: ");
  35.             fgets(name,MAX_NAME,stdin);
  36.             NoNewLine(name);      
  37.             current = FindInList(head, name);
  38.             if (current == NULL)
  39.             {
  40.                 printf("Name not found\n");
  41.                 break;
  42.             }
  43.             temp = current->next;
  44.             current->next = temp->next;
  45.             free(temp);
  46.  
  47.  
  48. /* Relevant functions....*/
  49.  
  50. /* Could be a macro -- shouldn't need explanation */
  51. void NoNewLine(
  52. char * string)
  53. {
  54.    if ((string) [strlen(string) -1] == '\n') \
  55.       (string) [strlen(string) -1] = '\0';
  56. }
  57.  
  58. /* Function to walk through list and check for a "match" */
  59. struct APLIST * FindInList(
  60. const struct APLIST *node,
  61. const char * name)
  62. {
  63.    struct APLIST * walk;
  64.    struct APLIST * previous;
  65.    for (walk = node, previous = NULL; walk; walk = walk->next)
  66.    {
  67.       if (strcmp(walk->patient.name,name) == 0)
  68.       {
  69.          return(previous);
  70.       }
  71.    }
  72.    previous = walk;
  73.    return(NULL);
  74. }
  75.           
  76.  
  77. Many thanks -- I'm truly perplexed by the constant return
  78. of "NULL" when I call this function from my switch/case.
  79.  
  80.  
  81.  
  82. ======================================================================
  83. *stking@acpub.duke.edu*| Such as they are, these are my opinions only;
  84.     *919-286-4476*     | not my employer's or internet provider's....
  85. ======================================================================
  86.  
  87.